package tomenttosa.prunuus.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public class CryptUtils {

	public static final String DESede = "DESede";

	public static String des3Encrypt(String key, String text) throws Exception {

		SecretKeySpec sks = new SecretKeySpec(Base64.decodeBase64(key), DESede);
		Cipher cipher = Cipher.getInstance(DESede);
		cipher.init(Cipher.ENCRYPT_MODE, sks);

		return Base64.encodeBase64String(cipher.doFinal(text.getBytes()));
	}

	public static String des3Decrypt(String key, String text) throws Exception {

		SecretKeySpec sks = new SecretKeySpec(Base64.decodeBase64(key), DESede);
		Cipher cipher = Cipher.getInstance(DESede);
		cipher.init(Cipher.DECRYPT_MODE, sks);

		return new String(cipher.doFinal(Base64.decodeBase64(text)));
	}

	public static String generate3desKey() throws Exception {

		KeyGenerator kg = KeyGenerator.getInstance(DESede);
		kg.init(112);
		byte[] key_bype = kg.generateKey().getEncoded();

		return Base64.encodeBase64String(key_bype);
	}
	
	public static String sha256Hex(String text){
		return DigestUtils.sha256Hex(text);
	}
}
